// 后台路由处理 /admin
const router = require('koa-router')();
const UserController = require('../controllers/admin/user');

const config = require('../config/config.js');

// api-登陆
router.post('/login', UserController.adminLogin );

// api-修改密码
router.post('/editPwd', UserController.editPwd );


// 验证登陆状态
router.use('/', async (ctx, next) => {
  if(ctx.request.url == "/" + config.admin){
    ctx.redirect('/' + config.admin +'/');
    return;
  }
  if(!ctx.session.username){
    // ctx.redirect('/admin/login');
    await ctx.render('admin/login');
    return;
  }

  ctx.state.username = ctx.session.username;
  ctx.state.userID = ctx.session.userID;

  await next();
})



// 后台首页
router.get('/', async (ctx) => {
  await ctx.render('admin/index');
})

// 修改密码
router.get('/edit_password', async (ctx) => {
  await ctx.render('admin/edit_password');
})



module.exports = router.routes();